// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-11 15:36:29
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2024-12-12 15:13:01
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Constraint\RBE2Parser.cpp
 */

#include "Import/BDFImport/BulkData/Constraint/RBE2Parser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Constraint/IRBE2Service.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (RBE2);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

/*
// file://./../../Doc/MarkDown/BulkData/RBE2/RBE2.md>
*/

enum RBE2key : std::uint8_t
{
    RBE2key_EID = 1,
    RBE2key_GN  = 2,
    RBE2key_CM  = 3,
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseRBE2 (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                            std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::RBE2Data tempData;

    // TODO(bug 001) m_dominId的正确填写
    tempData.m_domainId = 1;

    // 解析固定数据
    tempData.m_id         = convertToI64 (dividedEntryInfo[RBE2key_EID]);
    tempData.m_masterNode = convertToI64 (dividedEntryInfo[RBE2key_GN]);
    tempData.m_slaveDOFs  = convertToI64 (dividedEntryInfo[RBE2key_CM]);

    size_t const rowNum      = dividedEntryInfo.size () / 10;                      // 词条行数
    size_t       judgeRownum = rowNum - 1;
    int          lastindex   = findNonEmptyIndex (dividedEntryInfo, judgeRownum);  // 词条最后一个非空下标
    while (-1 == lastindex)
    {
        judgeRownum--;
        if (judgeRownum < 0)
        {
            throw std::runtime_error ("RBE2Parser: judgeRownum < 0");
        }
        lastindex = findNonEmptyIndex (dividedEntryInfo, judgeRownum);
    }

    size_t lastGMIndex = lastindex;  // 默认GM的最后一个下标是最后一非空下标

    // 判断是否存在线膨胀系数
    bool const isFloat = covertToScientificNotation (dividedEntryInfo[lastindex]);
    if (isFloat)
    {
        tempData.m_alpha = convertToReal (dividedEntryInfo[lastindex]);
        lastGMIndex      = lastindex - 1;  // 存在线膨胀系数，不是GM的下标是线膨胀系数下标
    }

    size_t const endColumnIndex   = 9;     // 续行符的下标
    size_t       beginColumnIndex = 4;     // 从节点的第一行从index=4开始
    for (size_t curRow = 0; curRow < rowNum; curRow++)
    {
        if (curRow != 0)
        {
            beginColumnIndex = 1;
        }

        for (size_t fieldsColumn = beginColumnIndex; fieldsColumn < endColumnIndex; fieldsColumn++)
        {
            // 计算当前字段的索引
            const size_t curIndex = curRow * 10 + fieldsColumn;

            if (curIndex > lastGMIndex)
            {
                break;
            }

            std::string& curStrKey = dividedEntryInfo[curIndex];
            // 检查当前字段是否为空白
            bool const isEmpty = is_all_space (curStrKey);

            // 如果当前字段为空白，跳过处理
            if (isEmpty)
            {
                continue;
            }

            // 不是空白则加入到m_slaveNodes中
            tempData.m_slaveNodes.push_back (convertToI64 (dividedEntryInfo[curIndex]));
            // 已经到最后一个GMindex，跳出循环
            if (curIndex == lastGMIndex)
            {
                break;
            }
        }
    }
    return dbServiceFactory->get<IRBE2Service> ()->Add (tempData);
}
